Paranoia.Browser = {};

local Paranoia = Paranoia;

--declarations
local MAX_LINE_COUNT = 16
local dbSearchResults = {}
local dbSearchIndex = {}

--events
function ParanoiaBrowser_OnListItemClick(self, button)
  if button == "LeftButton" then
    local listItemID = self:GetID()
    local listItemName = dbSearchIndex[listItemID + FauxScrollFrame_GetOffset(ParanoiaBrowserScrollFrame)]
    local player = ParanoiaData.DB.Players[Paranoia.Realm][listItemName]
    if not player then return; end
  end
end

function ParanoiaBrowser_Update()
  Paranoia.Browser.HidePlayerTooltip()
  local exists, name, level, class, guild, lastseen, kills, deaths, kos
  local numResults = getn(dbSearchIndex)
  local button, buttonText
	local listOffset = FauxScrollFrame_GetOffset(ParanoiaBrowserScrollFrame)
	local listIndex
	local showScrollBar = false
	if ( numResults > MAX_LINE_COUNT ) then
		showScrollBar = true
	end
	ParanoiaBrowserNumResults:SetText(string.format(WHO_NUM_RESULTS, numResults))
  for i=1, MAX_LINE_COUNT, 1 do
    listIndex = listOffset + i
    button = getglobal("ParanoiaBrowserButton"..i)
    button.listIndex = listIndex
    exists, name, level, class, guild, lastseen, kills, deaths, kos = Paranoia.Browser.GetSearchResultData(listIndex)
    if listIndex <= numResults then
      if exists then
        buttonText = getglobal("ParanoiaBrowserButton"..i.."Name")
        buttonText:SetText(name)
        buttonText = getglobal("ParanoiaBrowserButton"..i.."Class")
        local r,g,b = Paranoia:ClassColor(class)
        buttonText:SetText(class)
        buttonText:SetTextColor(r,g,b)
        buttonText = getglobal("ParanoiaBrowserButton"..i.."Level")
        buttonText:SetText(ParanoiaBrowser_LevelToString(level))
        buttonText = getglobal("ParanoiaBrowserButton"..i.."Guild")
        if not guild then guild = NONE; end
        buttonText:SetText(guild)
        buttonText = getglobal("ParanoiaBrowserButton"..i.."LastSeen")
        buttonText:SetText(Paranoia:TimeToString(time()-lastseen, true))
        buttonText = getglobal("ParanoiaBrowserButton"..i.."KillDeath")
        buttonText:SetText(tostring(kills).."/"..tostring(deaths))
        buttonText = getglobal("ParanoiaBrowserButton"..i.."KoS")
        if kos == true then buttonText:SetText(YES); else buttonText:SetText(NO); end
        button:Show()
      end
    else
      button:Hide()
		end
		-- Highlight the correct who
		if ParanoiaBrowserScrollFrame.selectedPlayer == listIndex then
			button:LockHighlight()
		else
			button:UnlockHighlight()
		end
		
	end

	if not ParanoiaBrowserScrollFrame.selectedIndex then
--~ enable/disable kos buttons here, once we've got them working
	else
		ParanoiaBrowserScrollFrame.selectedPlayer = Paranoia.Browser.GetSearchResultData(ParanoiaBrowserScrollFrame.selectedIndex)
	end
	FauxScrollFrame_Update(ParanoiaBrowserScrollFrame, numResults, MAX_LINE_COUNT, 16)
end


--searches the database by player name and adds matches to the dbSearchResults table.
function Paranoia.Browser.SearchDatabase(searchParams)
  dbSearchResults = {}
  if searchParams == nil then searchParams = ""; end
  for playerName, _ in pairs(ParanoiaData.DB.Players[Paranoia.Realm][Paranoia.OppFactionEng]) do
    local isMatch = false
    if searchParams == "" then
      isMatch = true
    else
      if string.find(" "..playerName.." ", searchParams) then isMatch = true; end
    end
    if isMatch then
      dbSearchResults[playerName] = ParanoiaData.DB.Players[Paranoia.Realm][Paranoia.OppFactionEng][playerName]
    end
  end
  Paranoia.Browser.GenerateSearchIndex()
  Paranoia.Browser.Update()
end

--assigns each search result a number, or index, so we can loop through them and display a certain range in the scrollframe.
function Paranoia.Browser.GenerateSearchIndex()
  dbSearchIndex = {}
  local i = 0
	for resultName, _ in pairs(dbSearchResults) do
    dbSearchIndex[i] = resultName
    i = i + 1
  end
end

--returns name, level, class, guild, lastseen, kills, deaths, and kos flag data for the given search result (by name or index)
function Paranoia.Browser.GetSearchResultData(result)
  local resultName, pName, pLevel, pClass, pGuild, pLastSeen, pKills, pDeaths, pKos
  if dbSearchResults == nil then Paranoia.Browser.SearchDatabase(); end
  if type(result) == "number" then resultName = dbSearchIndex[result]; else resultName = result; end
  if not resultName then return false; end
  pName = resultName
  pLevel = dbSearchResults[resultName][1]
  pClass = dbSearchResults[resultName][2]
  pGuild = dbSearchResults[resultName][3]
  pLastSeen = dbSearchResults[resultName][4]
  pKos = dbSearchResults[resultName][5]
  pKills = dbSearchResults[resultName][6]
  pDeaths = dbSearchResults[resultName][7]
  return true, pName, pLevel, pClass, pGuild, pLastSeen, pKills, pDeaths, pKos
end


--tooltip
function ParanoiaBrowser_ShowPlayerTooltip(self)
  local listItemID = self:GetID()
  local listItemName = dbSearchIndex[listItemID + FauxScrollFrame_GetOffset(ParanoiaBrowserScrollFrame)]
  local player = ParanoiaData.DB.Players[Paranoia.Realm][Paranoia.OppFactionEng][listItemName]
  local playerNameColorCode = "|cFFFFBBBB"
  if player[5] == true then playerNameColorCode = "|cFFFF0000"; end
  GameTooltip:SetOwner(self, "ANCHOR_CURSOR")
  GameTooltip:SetText(playerNameColorCode..listItemName.."|r\n")
  GameTooltip:Show()
end

function Paranoia.Browser.HidePlayerTooltip()
  GameTooltip:Hide()
end


--utility
function Paranoia.Browser.LevelToString(level) --Converts a numerical level from database to a string level.
  if level == 80 then
    return "80"
  elseif level == 0 then
    return "-"
  elseif level and level ~= 0 then
    return tostring(level).."+"
  else
    return "-"
  end
end